/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jpa.exceptions.NonexistentEntityException;

public class ServletBerita extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, NonexistentEntityException {
        String halaman = request.getParameter("halaman"); //variabel bantu untuk melakukan mapping halaman
        RequestDispatcher rd = null;

        if (halaman != null) {

            if (halaman.equals("ditambahkan")) {
                ServletBeritaHelper suh = new ServletBeritaHelper(request);
                rd = request.getRequestDispatcher(suh.create());
            } else if (halaman.equals("daftarberita")) {
                ServletBeritaHelper suh = new ServletBeritaHelper(request);
                rd = request.getRequestDispatcher(suh.hapusBerita());
            } else if (halaman.equals("tambahberita")) {
                rd = request.getRequestDispatcher("beritaTambah.jsp");
            } else if (halaman.equals("berita")) {
                rd = request.getRequestDispatcher("berita.jsp");
            } else if (halaman.equals("editBerita")) {
                ServletBeritaHelper suh = new ServletBeritaHelper(request);
                rd = request.getRequestDispatcher(suh.bukaEditBerita());
            } else if (halaman.equals("sudaheditberita")) {
                ServletBeritaHelper suh = new ServletBeritaHelper(request);
                rd = request.getRequestDispatcher(suh.submitEditBerita());
            } else if (halaman.equals("bacaBerita")) {
                ServletBeritaHelper suh = new ServletBeritaHelper(request);
                rd = request.getRequestDispatcher(suh.bacaBerita());
            } 
            else if (halaman.equals("awal")) {
                rd = request.getRequestDispatcher("index.jsp"); }
           
        } else {
            rd = request.getRequestDispatcher("index.jsp");
        }
        rd.forward(request, response);

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(ServletBerita.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(ServletBerita.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
